$(function () {
    inputRadioSel($(".i-checks"),selectCall);//查询所有单选按钮，回调方法selectCall
    selectData('COURSE');
    selectGroomPlace('COURSE');
    $('body').on('click', '#onBack', function () {
        onBackHistory();
    });
    $('body').on('click','.deleteData',function () {
        var Id =  $(this).parents('tr').data('id');
        var  obj = $(this).parents('tr');
        deleteSelectData(Id,obj);
    });
    savePlaceData();
});

/**
 * 取消
 */
function onBackHistory() {
    window.history.go(-1);
}

/**
 * 选择回调
 */
function selectCall(em) {
    if(em.name=='dataType'){
        var typeName ='';
        var dataType='';
        if(em.value=='COURSE'){
            typeName ='课程';
            dataType='COURSE';
        }
        if(em.value=='USER'){
            typeName ='学员';
            dataType='USER';
        }
        if(em.value=='TEACHER'){
            typeName ='讲师';
            dataType='TEACHER';
        }
        if(em.value=='ARTICLE'){
            typeName ='新闻资讯';
            dataType='ARTICLE';
        }
        if(em.value=='BOOK'){
            typeName ='图书';
            dataType='BOOK';
        }
        if(em.value=='PACKAGE'){
            typeName ='套餐';
            dataType='PACKAGE';
        }
        if(em.value=='LIVE'){
            typeName='直播';
            dataType='LIVE';
        }
        $('#recommendedData').html('推荐'+typeName);
        $('#selectData').html('<i class="fa fa-plus"></i> &nbsp;选择'+typeName);
        selectData(dataType);
        $('#data-content').html('');
        $("#dataType").html('');
        selectGroomPlace(dataType);
    }
}

function selectData(dataType) {
    $('body').on('click', '#selectData', function () {
        parent.layer.closeAll();
        if(dataType=='COURSE'){
           selectCourse({
               selectType:"checkbox",
               type:1,
               callback:'selectCourseCallback'
           });
        }
        if(dataType=='USER'){
            selectUser("checkbox","");
        }
        if(dataType=='ARTICLE'){
            selectArticle("checkbox",'');
        }
        if(dataType=='TEACHER'){
            selectTeacher("checkbox",'','selectTeacherCallback','1');
        }
        if(dataType=='BOOK'){
            selectBook({
                selectType:'checkbox',
                callback:'selectBookCallback'
            });
        }
        if(dataType=='PACKAGE'){
            selectPackage("checkbox",'');
        }
        if(dataType =='LIVE'){
            selectCourse({
                selectType:"checkbox",
                type:3,
                callback:'selectCourseCallback'
            });
        }
    });
}

/**
 * 用户回调
 * @param result
 */
function selectUserCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.userName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                $('#data-content').append(' <tr data-id="'+id+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);">删除</a></td>'+
                    '</tr>');
            }
        });
        $('input[name="dataIds"]').val(dataIds);
    }
}

/**
 * 课程回调
 * @param result
 */
function selectCourseCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.courseName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                $('#data-content').append(' <tr data-id="'+id+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);">删除</a></td>'+
                    '</tr>');
            }
        });
        $('input[name="dataIds"]').val(dataIds);
    }
}

/**
 * 讲师回调
 * @param result
 */
function selectTeacherCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.teacherName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                $('#data-content').append(' <tr data-id="'+id+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);">删除</a></td>'+
                    '</tr>');
            }
        });
        $('input[name="dataIds"]').val(dataIds);
    }
}

/**
 * 图书回调
 * @param result
 */
function selectBookCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.bookName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                $('#data-content').append(' <tr data-id="'+id+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);">删除</a></td>'+
                    '</tr>');
            }
        });
        $('input[name="dataIds"]').val(dataIds);
    }
}

/**
 * 套餐回调
 * @param result
 */
function selectPackageCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.courseName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                $('#data-content').append(' <tr data-id="'+id+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);">删除</a></td>'+
                    '</tr>');
            }
        });
        $('input[name="dataIds"]').val(dataIds);
    }
}


/**
 * 套餐回调
 * @param result
 */
function selectArticleCallback(result) {
    if(result!=null && result.length>0) {
        var dataIds = $('input[name="dataIds"]').val();
        if(dataIds==null || dataIds == '' || dataIds == 'undefined'){
            dataIds = ",";
        }
        $.each(result, function (index, data) {
            var id = data.id;
            var name = data.articleName;
            if (dataIds.indexOf("," + id+ ",") == -1 && dataIds.indexOf(id + ",") == -1) {
                dataIds += id + ","
                $('#data-content').append(' <tr data-id="'+id+'">'+
                    '<td class="text-center">'+id+'</td>'+
                    '<td class="text-center">'+name+'</td>'+
                    '<td class="text-center"><a class="c-master deleteData"  href="javascript:void(0);">删除</a></td>'+
                    '</tr>');
            }
        });
        $('input[name="dataIds"]').val(dataIds);
    }
}
function deleteSelectData(Id,obj) {
    var dataIds=',';
    var datas = $("#dataIds").val();
    var dataId = datas.split(',');
    for(var i=0;i<dataId.length;i++){
        if(dataId[i]!=null&&dataId[i]!=''&&dataId[i]!=Id){
            dataIds +=dataId[i]+",";
        }
    }
    if(dataIds.length>1){
        $("#dataIds").val(dataIds);
    }else{
        $("#dataIds").val("");
    }
    obj.remove();
}

function selectGroomPlace(dataType) {
    $.ajax({
        url:baseloaction+"/admin/op/groom/queryOpGroomPlaceByDataType",
        type:"post",
        data:{"dataType":dataType},
        dataType:"json",
        success:function (result) {
            if(result.success==true){
                var groomPlaces = result.entity;
                console.log(groomPlaces);
                var i;
                var context = '';
                for(i in groomPlaces){
                    context+='<label style="padding: 0"><input name="id" data-id="'+groomPlaces[i].id+'"  class="i-checks"   type="checkbox" value="'+groomPlaces[i].dataType+'"  />'+groomPlaces[i].placeName+'</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
                }
                $("#dataType").html(context);
                checksFun();
            }else{
                icModale(0, "系统繁忙，请稍后再试");
            }
        },
        error:function (error) {
            icModale(0, "系统繁忙，请稍后再试");
        }
    });
}

function savePlaceData() {
    $('body').on('click','#submitPlaceData',function () {
        var place = $('input[name="id"]:checked');
        var dataType = $('input[name="dataType"]:checked').val();
        var placeIds = '';
        var dataIds =$('#dataIds').val();
        place.map(function () {
            placeIds+=$(this).data('id')+','
        });
        $.ajax({
            url:baseloaction+"/admin/op/groom/saveOpGroomDataBatch",
            type:"post",
            data:{"dataIds":dataIds,"dataType":dataType,"placeIds":placeIds},
            dataType:"json",
            success:function (result) {
                if(result.success==true){
                    icModale(0,result.message);
                    window.location.href=baseloaction+"/admin/op/groom/queryGroomPlaceList"
                }else{
                    icModale(0, result.message);
                }
            },
            error:function (error) {
                icModale(0, "系统繁忙，请稍后再试");
            }
        });
    });
}
